﻿Public Class File_Browser
    Private mRootPath As String = Replace(Debugger.DirMobs, "\Databases\Mobiles\Mobs.xml", "")
    Private LoadFile As String
    Property RootPath As String
        Get
            Return mRootPath
        End Get
        Set(ByVal value As String)
            mRootPath = value
        End Set
    End Property
#Region "Resize"
    Private Sub ResizeResources(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        ResourceTree.Height = Me.Height - 61
        ResourceTree.Width = Me.Width - 17
        Button1.Top = Me.Height - 60
        Button1.Width = Me.Width - 17
    End Sub
    Private Sub CheckValidSize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ResizeEnd
        If Me.Width <= 100 Then
            Me.Width = 100
            ResourceTree.Width = Me.Width - 17
        End If
        If Me.Height <= 300 Then
            Me.Height = 300
            ResourceTree.Height = Me.Height - 61
        End If
    End Sub
#End Region
#Region "File Growth and Selection"
    Private Sub DetermineSelection(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles ResourceTree.AfterSelect
        If InStr(e.Node.Text, ".TG") > 0 Then
            Button1.Enabled = True
            LoadFile = Replace(e.Node.FullPath, "//", "/")
        ElseIf InStr(e.Node.Text, ".xml") > 0 Then
            Button1.Enabled = True
            LoadFile = Replace(e.Node.FullPath, "\\", "\")
        Else
            Button1.Enabled = False
        End If
    End Sub
    Private Sub ResourceBuild(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ResourceTree.Height = Me.Height - 61
        ResourceTree.Width = Me.Width - 17
        Button1.Top = Me.Height - 60
        Button1.Width = Me.Width - 17
        ' when our component is loaded, we initialize the TreeView by  adding  the root node
        Dim mRootNode As New TreeNode
        mRootNode.Text = RootPath
        mRootNode.Tag = RootPath
        mRootNode.Nodes.Add("*DUMMY*")
        ResourceTree.Nodes.Add(mRootNode)
    End Sub
    Private Sub BeforeXpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles ResourceTree.BeforeExpand
        ' clear the argNode so we can re-populate, or else we end up  with duplicate nodes
        e.Node.Nodes.Clear()
        ' get the directory representing this node
        Dim mNodeDirectory As New IO.DirectoryInfo(e.Node.Tag)
        ' add each directory from the file system that is a child of  the argNode that was passed in
        For Each mDirectory As IO.DirectoryInfo In mNodeDirectory.GetDirectories
            ' declare a TreeNode for this directory
            Dim mDirectoryNode As New TreeNode
            ' store the full path to this directory in the directory  TreeNode's Tag property
            mDirectoryNode.Tag = mDirectory.FullName
            ' set the directory TreeNodes's display text
            mDirectoryNode.Text = mDirectory.Name
            ' add a dummy TreeNode to the directory node so that it  is expandable
            mDirectoryNode.Nodes.Add("*DUMMY*")
            ' add this directory treenode to the treenode that is  being populated
            If InStr(mDirectoryNode.Tag, "svn") Or InStr(mDirectoryNode.Tag, "ebsite") Then
                'make sure that subversion folders and the website folder doesn't show up if the developer is
                'an admin with full rights and has access to these folders. They're not utilized in Clay anyways
                'so there's no need for them to be available in the File Browser.
                'this is currently setup this way because I plan on adding options to toggle this later on
            Else
                e.Node.Nodes.Add(mDirectoryNode)
            End If
        Next
        For Each mdirectory As IO.FileInfo In mNodeDirectory.GetFiles
            ' declare a TreeNode for this directory
            Dim mFileNode As New TreeNode
            ' store the full path to this directory in the directory  TreeNode's Tag property
            mFileNode.Tag = mdirectory.FullName
            ' set the directory TreeNodes's display text
            mFileNode.Text = mdirectory.Name
            If InStr(mdirectory.Name, ".TG") > 0 Then
                mFileNode.BackColor = Color.LightGreen
            ElseIf InStr(mdirectory.Name, ".xml") > 0 Then
                mFileNode.BackColor = Color.LightBlue
            End If
            ' add this directory treenode to the treenode that is  being populated
            e.Node.Nodes.Add(mFileNode)
        Next
    End Sub
    Private Sub BeforeCollapse(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles ResourceTree.BeforeCollapse
        ' clear the node that is being collapsed
        e.Node.Nodes.Clear()
        ' add a dummy TreeNode to the node being collapsed so it is expandable
        e.Node.Nodes.Add("*DUMMY*")
    End Sub
#End Region
    Private Sub OpenFile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Debugger.Current = "AtlasNodes" Then Debugger.AtlasNodesData = Editor.PAD.Text
        If Debugger.Current = "Mobiles" Then Debugger.MobData = Editor.PAD.Text
        If Debugger.Current = "Items" Then Debugger.ItemData = Editor.PAD.Text
        If Debugger.Current = "Emotes" Then Debugger.EmoteData = Editor.PAD.Text
        If Debugger.Current = "Misc" Then Debugger.MiscData = Editor.PAD.Text
        If Debugger.Current = "Abilities" Then Debugger.AbilitiesData = Editor.PAD.Text
        If Debugger.Current = "Scripts" Then Debugger.ScriptData = Editor.PAD.Text
        Debugger.ResumeMisc.Enabled = True
        Debugger.Current = "Misc"
        Debugger.DirMisc = LoadFile
        Debugger.MiscData = Debugger.GetFileContents(Debugger.DirMisc, Debugger.serr)
        Debugger.FormatTextColor(Debugger.MiscData)
        Debugger.SaveMiscM.Enabled = True
        Debugger.CloseMiscM.Enabled = True
        Debugger.updatestatus()
        If Debugger.EditorOpen = False Then
            Editor.Show()
            Debugger.EditorOpen = True
        End If
    End Sub
End Class